var rootPath = getRootPath();
var oriPhone;

$(function() {
	//wx---start
//	var ua = navigator.userAgent.toLowerCase();
//	var isWeixin = ua.indexOf('micromessenger') != -1;
//	if(isWeixin){
//		$("#wechatAddress").css("display","flex");
//		getWXInfoMation();
//	}else{
//		$("#wechatAddress").css("display","none");
//	}
	$("#wechatAddress").css("display","none");
	
	//wx---end
	$("#checkbox").prop("checked",false);
	
	$("#Addr").bind("input propertychange",function(){
	});
	
    // 地址选择器遮罩层打开与关闭
	$("#areaLabel").click(function(e){
	    $("#addressSelectWrapper").show();
	    e.stopPropagation();
	});
	$(document).click(function () {
        $("#addressSelectWrapper").hide();
    });
	$("#cancel").click(function () {
        $("#addressSelectWrapper").hide();
    });
    $("#addressSelect").click(function (e) {
		e.stopPropagation();
    });
    initAddress();
    
  	//获取编辑地址的参数 
 	var id = getUrlParam("id");
	getData(id);
});
/**
 * 初始化地址三级联动
 * @returns
 */
function initAddress() {
    $("#Addr").cityLinkage({
        containerId: 'addressSelectWrapper',
        getSelectedCode:function(){return $("#Addr").data("code");},
        callback:function(data) {
			$("#provinceName").val(data.province.name);
			$("#cityName").val(data.city.name);
			$("#countryName").val(data.area.name);
            $("#Addr").val(data.addr).data("code",data.area.code);
        		hideMsg("Addr");
        }
    });
}
/**
 * 初始化数据
 * @param id
 * @returns
 */
function getData(id){
	if( !id || $.trim(id) == ''){
		return;
	}
	 $.ajax({
			url: rootPath + '/userAddress/search/'+id,
			type: 'GET',
			contentType: 'application/json;charset=UTF-8',
			success: res=> {
				var code = res.returnCode;
				if(code == "0000"){
					editData(res.returnObject);
				}else{
					//查询错误~~~就不渲染页面了
				}
				 
			},
			error:res=>{
				//..
			}
	 });
}
//回调值
function editData(obj){
	if($.isEmptyObject(obj)){
		return;
	}
	if(obj.uaId!=null && obj.uaId!=""){
		$("#uaId").val(obj.uaId);
	}
	if(obj.detailInfo!=null && obj.detailInfo!=""){
		$("#detailInfo").val(obj.detailInfo);
	}
	if(obj.userName!=null && obj.userName!=""){
		$("#userName").val(obj.userName);
	}
	if(obj.provinceName!=null && obj.provinceName!=""){
		$("#provinceName").val(obj.provinceName);
		$("#Addr").val(obj.provinceName);
	}
	if(obj.cityName!=null && obj.cityName!=""){
		
		$("#cityName").val(obj.cityName);
		
		var str = $("#Addr").val();
		str += obj.cityName;
		$("#Addr").val(str);
	}
	if(obj.countryName!=null && obj.countryName!=""){
		
		$("#countryName").val(obj.countryName);
		
		var str = $("#Addr").val();
		str += obj.countryName;
		$("#Addr").val(str);
	}
	if(obj.telNumber!=null && obj.telNumber!=""){
		$("#telNumber").val(obj.telNumber);
	}
	
	
	if(obj.isDefault == true || obj.isDefault == "true"){
		$("#checkbox").prop("checked",true);
	}else{
		$("#checkbox").prop("checked",false);
	}
	
	//匹配省市区下拉选框
	//$("#Addr").data("code","130102");
}



//wx部分 JS
function getWXInfoMation(){
	var link = location.href;
	 $.ajax({
		 url: rootPath +"/wx/getInfoMation",
		 type:"POST",
		 async:false,
		 contentType:"application/json; charset=utf-8",
		 dataType:"json",
		 data:JSON.stringify({
			 "url":link
	        }),
		 success:function(res){
			 if("0000" == res.returnCode){
				 var data = res.returnObject;
				 wx.config({
					 debug: false,
					 appId: data.appid,
					 timestamp:data.timestamp,
					 nonceStr:data.noncestr,
					 signature:data.signature,
					 jsApiList: ['openAddress']
				 });
			 }
		 },
		 fail:function(res){
			 
		 }
	 });
}
//wx部分 JS end
